﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace zazzle.net
{
    public class WebUtils
    {

        public static int CoalParms(int? parm, string qs, int cfg)
        {
            //check parm
            if (parm.HasValue) return parm.Value;
            //check qs
            if (qs != null && qs.Length > 0)
            {
                string tmp = (HttpContext.Current.Request.QueryString[qs] ?? "").ToString().ToLower();
                int tmpi;
                if (tmp.Length > 0 && Int32.TryParse(tmp, out tmpi))
                {
                    return tmpi;
                }
            }
            //default to config
           return cfg;
        }
        public static string CoalParms(string parm, string qs, string cfg)
        {
            //check parm
            if (parm !=null) return parm;
            //check qs
            if (qs != null && qs.Length > 0)
            {
                string tmp = (HttpContext.Current.Request.QueryString[qs] ?? "").ToString().ToLower();
                if (tmp.Length > 0)
                {
                    return tmp;
                }
            }
            //default to config
            return cfg;
        }
        public static bool CoalParms(bool? parm, string qs, bool cfg)
        {
            //check parm
            if (parm.HasValue) return parm.Value;
            //check qs
            if (qs != null && qs.Length > 0)
            {
                string tmp = (HttpContext.Current.Request.QueryString[qs] ?? "").ToString().ToLower();
                bool tmpb;
                if (tmp.Length > 0 && Boolean.TryParse(tmp, out tmpb))
                {
                    return tmpb;
                }
            }
            //default to config
            return cfg;
        }
    }
}
